package com.carful.xigua.modules.common.advice;


import com.carful.xigua.modules.common.entity.Rest;
import com.carful.xigua.modules.common.entity.RestBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingRequestValueException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * Date 2024/1/22 10:58
 * Description 全局异常处理
 */

@RestControllerAdvice
@Order(1)
@Slf4j
public class GlobalExceptionAdvice {
    private static final String BAD_REQUEST_MSG = "客户端请求参数错误";

    // 请求未到控制层错误
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Rest<String> HttpMessageNotReadableExceptionHandler(HttpMessageNotReadableException e) {
        log.error(e.getMessage());
        return RestBody.build(HttpStatus.BAD_REQUEST.value(), e.getMessage(), BAD_REQUEST_MSG + e.getMessage(), null);
    }


    // <1> 处理 form data方式调用接口校验失败抛出的异常
    @ExceptionHandler(BindException.class)
    public Rest<List<String>> bindExceptionHandler(BindException e) {
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        List<String> collect = fieldErrors.stream()
                .map(o -> o.getField() + o.getDefaultMessage())
                .collect(Collectors.toList());
        log.error(e.getNestedPath());
        log.error(e.getMessage());
        return RestBody.build(HttpStatus.BAD_REQUEST.value(), collect, BAD_REQUEST_MSG + collect, null);
    }

    // <2> 处理 json 请求体调用接口校验失败抛出的异常
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Rest<List<String>> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        List<String> collect = fieldErrors.stream()
                .map(o -> o.getField() + o.getDefaultMessage())
                .collect(Collectors.toList());
        log.error(e.getNestedPath());
        log.error(e.getMessage());
        return RestBody.build(HttpStatus.BAD_REQUEST.value(), collect, BAD_REQUEST_MSG + collect, null);
    }

    // <3> 处理单个参数校验失败抛出的异常
    @ExceptionHandler(MissingRequestValueException.class)
    public Rest<MissingRequestValueException> constraintViolationExceptionHandler(MissingRequestValueException e) {
        return RestBody.build(HttpStatus.BAD_REQUEST.value(), e, e.getMessage(), null);
    }

//    // 登录认证错误
//    @ExceptionHandler(AuthenticationException.class)
//    public Rest<AuthenticationException> authenticationExceptionHandler(AuthenticationException e) {
//        return RestBody.build(400, e, e.getMessage(), null);
//    }

}